package com.wthink.newMain.settings.config.inner

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentNewNetconfigBinding
import com.wthink.newMain.settings.config.inner.netconfig.ConfigNewFragment

import com.wthink.newMain.settings.config.inner.viewmodel.NetConfigNewViewmodel
import kotlinx.android.synthetic.main.fragment_new_netconfig.*

@SuppressLint("ResourceAsColor")
class NetConfigNewFragment : Fragment() {

    private val viewModel by viewModels<NetConfigNewViewmodel>()
    private lateinit var binding: FragmentNewNetconfigBinding

         var fragment1: ConfigNewFragment? = null
    var fragment2: ConfigNewFragment? = null
    var fragment3: ConfigNewFragment? = null
    var fragment4: ConfigNewFragment? = null
    var fragment5: ConfigNewFragment? = null
    var fragment6: ConfigNewFragment? = null
    var fragment7: ConfigNewFragment? = null
    var fragment8: ConfigNewFragment? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_new_netconfig,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFragment()
    }

    private fun initFragment() {
        fragment1 = ConfigNewFragment()
        val bundle1 = Bundle()
        bundle1.putInt("index", 0)
        fragment1!!.arguments = bundle1

        fragment2 = ConfigNewFragment()
        fragment3 = ConfigNewFragment()
        fragment4 = ConfigNewFragment()
        fragment5 = ConfigNewFragment()
        fragment6 = ConfigNewFragment()
        fragment7 = ConfigNewFragment()
        fragment8 = ConfigNewFragment()

        val name = "服务器"
        val fragments = arrayOf(
            fragment1, fragment2, fragment3, fragment4, fragment5,
            fragment6, fragment7, fragment8
        )
        vp.adapter = object : FragmentPagerAdapter(
            activity?.supportFragmentManager!!,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getItem(position: Int): Fragment = fragments[position]!!

            override fun getCount(): Int = 8

            override fun getPageTitle(position: Int): CharSequence? {
                return "$name${position + 1}"
            }
        }
        table.setupWithViewPager(vp)
    }
}